package com.zzl.car.service;

import com.zzl.car.context.ZdpJwtUserContext;
import com.zzl.car.entity.CarBaseInfo;
import com.zzl.car.entity.CarDetailInfo;
import com.zzl.car.entity.LoveCarScore;
import com.zzl.car.entity.User;
import com.zzl.car.exception.ZdpException;
import com.zzl.car.mapper.CarBaseInfoMapper;
import com.zzl.car.mapper.CarDetailInfoMapper;
import com.zzl.car.mapper.ZzwCarDetailInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CarDetailInfoService {

    @Autowired
    private CarDetailInfoMapper carDetailInfoMapper;
    @Autowired
    private CarBaseInfoMapper carBaseInfoMapper;
    @Autowired
    private ZzwCarDetailInfoMapper zzwCarDetailInfoMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private LoveCarScoresService loveCarScoresService;

    /*
    * 根据id获取到记录
    *   封装 车辆基础信息 CarBaseInfo
    * */
    public CarDetailInfo getCarDetailInfoById(Integer id){
        CarDetailInfo carDetailInfo = carDetailInfoMapper.getCarDetailInfoById(id);
        carDetailInfo.setCarBaseInfo(carBaseInfoMapper.getCarBaseInfoById(carDetailInfo.getCarBaseInfoId()));
        //System.out.println(carDetailInfo);
        return carDetailInfo;
    }
   /*
   * 获取到车的引擎号的车牌号
   * */
   public CarDetailInfo getCarPlateAndCarEngine(Integer id){
       return carDetailInfoMapper.getCarPlateAndEngineById(id);
   }

   /*
   * 获取到所有可以租赁的车辆的详细信息
   * */
   public List<CarDetailInfo> getAllCar(){
        List<CarDetailInfo> carDetailInfos = carDetailInfoMapper.getAllCarAndStateZero();
        for(int i =0;i<carDetailInfos.size();i++){
            CarDetailInfo c = carDetailInfos.get(i);
            c.setCarBaseInfo(carBaseInfoMapper.getCarBaseInfoById(c.getCarBaseInfoId()));
        }
        return carDetailInfos;
   }

   /*
   * 在爱车指数分的限制下，获取到所有可以租赁的车辆的详细信息
   * */
   public List<CarDetailInfo> getAllCarByLoveCarScore(){
       // 获取用户信息
       String telephone = ZdpJwtUserContext.getCurrentName();
       // 查询用户
       User user = userService.getUserInfo(telephone);
       System.out.println(user);
       // 查询该爱车指数分所在区间
       LoveCarScore userLoveCarScoreInterval = loveCarScoresService.getUserLoveCarScoreInterval(user.getLoveCarScore());
       List<CarDetailInfo> carDetailInfos = carDetailInfoMapper.getAllCarAndStateZeroAndLoveCarScore( ( (double)userLoveCarScoreInterval.getRent() ) );
       for(int i =0;i<carDetailInfos.size();i++){
           CarDetailInfo c = carDetailInfos.get(i);
           c.setCarBaseInfo(carBaseInfoMapper.getCarBaseInfoById(c.getCarBaseInfoId()));
       }
       return carDetailInfos;
   }

   /*
   * 获取到指定商标和类型以及租金的可租赁的车辆
   * */
   public List<CarDetailInfo> getCarByBranchAndTypeAndRent(String branch,String type,double rent){
       // 要加入用户的爱车指数分
       String telephone = ZdpJwtUserContext.getCurrentName();
       // 查询用户
       User user = userService.getUserInfo(telephone);
       // 查询该爱车指数分所在区间
       LoveCarScore userLoveCarScoreInterval = loveCarScoresService.getUserLoveCarScoreInterval(user.getLoveCarScore());

        List<CarBaseInfo> carBaseInfos = carBaseInfoMapper.getCarBaseInfoByBranchAndType(branch,type);
        List<CarDetailInfo> result = new ArrayList<>();
        for(CarBaseInfo c:carBaseInfos){
            List<CarDetailInfo> carByRent = carDetailInfoMapper.getCarByRent(c.getId(), rent,(double)userLoveCarScoreInterval.getRent());
            for(int i=0;i<carByRent.size();i++){
                carByRent.get(i).setCarBaseInfo(c);
            }
            result.addAll(carByRent);
        }
        return result;
   }
   /*
   * 更新车辆信息
   * */
   public CarDetailInfo updateCarDetailInfo(CarDetailInfo c){
       int i = zzwCarDetailInfoMapper.modifyCarDetailInfoById(c);
       if(i==0) {
           throw new ZdpException(1011, "车辆信息更新失败");
       }
       return c;
   }

   /*
   * 更新车辆状态
   * */
   public Integer updateCarState(Integer carId){
       Integer integer = carDetailInfoMapper.updateCarState(carId);
       if(integer == null){
           throw new ZdpException(1011,"车辆信息更新失败");
       }
       return integer;
   }

   /*
   * 根据传入的车辆id 和 状态来更新车辆状态
   * */
   public Integer updateCarStateByUser(Integer state,Integer carId){
       Integer integer = carDetailInfoMapper.updateCarStateByUser(state, carId);
       if(integer == null || integer ==0){
           throw new ZdpException(1011,"车辆信息更新失败");
       }
       return integer;
   }


}
